﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;

namespace NewWords
{
    class ScrollText
    {
        private TextBox textControl;
        private string[] SourceCollection;
        private Timer timer;

        public int TimerInterval
        {
            get;
            set;
        }

        public int ForwardSteps
        {
            get;
            set;
        }

        public int ScrollingLength
        {
            get;
            set;
        }

        public int SeparateSpaces
        {
            get;
            set;
        }

        public int CurrentIndex
        {
            get;
            set;
        }

        public int CurrentOffset
        {
            get;
            set;
        }

        public bool IsStarted
        {
            get;
            private set;
        }

        public Queue<char> ScrollingQueue
        {
            get;
            set;
        }

        public ScrollText(TextBox ctrl, string[] source)
        {
            textControl = ctrl;
            ScrollingLength = 50;
            textControl.Text = new string(' ', ScrollingLength);
            SourceCollection = source;
            ScrollingQueue = new Queue<char>();
            TimerInterval = 1000;
            ForwardSteps = 1;
            SeparateSpaces = 5;
            CurrentIndex = 0;
            timer = new Timer();
            timer.Tick += new EventHandler(timer_Tick);
        }

        void EnqueueString( string str)
        {
            foreach (char item in str)
            {
                ScrollingQueue.Enqueue(item);
            }
        }
        void timer_Tick(object sender, EventArgs e)
        {
            while (ScrollingQueue.Count() < ForwardSteps && CurrentIndex < SourceCollection.Length)
            {
                EnqueueString(new string(' ', SeparateSpaces));
                EnqueueString(SourceCollection[CurrentIndex++]);
            }

            List<char> chars = new List<char>();
            for (int i = 0; i < ForwardSteps; i++)
            {
                try
                {
                    chars.Add(ScrollingQueue.Dequeue());
                }
                catch (InvalidOperationException exp)
                {
                    Debug.WriteLine(exp.ToString());
                    break;
                }
            }
            this.textControl.Text = (this.textControl.Text + new string(chars.ToArray())).Substring(chars.Count);
            Debug.Assert(this.textControl.Text.Length <= ScrollingLength);
            //Debug.WriteLine(this.textControl.Text.Length+" : "+this.textControl.Text);
        }

        public void Start()
        {
            if (null==timer)
            {
                timer = new Timer();
                timer.Interval = TimerInterval;
            }
            timer.Start();
            IsStarted = true;
        }

        public void Stop()
        {
            if (null!=timer)
            {
                timer.Stop();
            }
            IsStarted = false;
        }
    }
}
